﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Editor.Core.Data;
using AG.Editor.Core;
using AG.Editor.Core.Metadata;

namespace AG.Editor.MapUI.Windows
{
    public partial class AGECreateMapWindow : Form
    {
        public AGMap Map { get; private set; }
        public AGECreateMapWindow()
        {
            InitializeComponent();

            foreach (AGTerrain terrain in AGEContext.Current.EProject.Terrains)
            {
                if (terrain.IsSimple())
                {
                    _ctlListTerrain.Items.Add(terrain);
                }
            }
        }

        private void _ctlBtnCreate_Click(object sender, EventArgs e)
        {
            AGTerrain selectItem = _ctlListTerrain.SelectedItem as AGTerrain;
            if (selectItem == null)
            {
                return;
            }

            try
            {
                ushort mapid = ushort.Parse(_ctlEditId.Text);
                ushort row = ushort.Parse(_ctlEditRow.Text);
                ushort col = ushort.Parse(_ctlEditCol.Text);
                string name = _ctlEditName.Text;

                Map = new AGMap();
                Map.Id = mapid;
                Map.Row = row;
                Map.Col = col;
                Map.Name = name;
                Map.Tiles = new AGMapTile[Map.Row * Map.Col];

                for (int rowIndex = 0; rowIndex < row; rowIndex++)
                {
                    for (int colIndex = 0; colIndex < col; colIndex++)
                    {
                        AGMapTile tile = new AGMapTile();
                        Map.Tiles[rowIndex * Map.Col + colIndex] = tile;
                        tile.TerrainId = selectItem.ForegroundId;
                        tile.TileId = selectItem.Tiles.First().Id;
                        tile.TileIndex = (byte)new Random().Next(0, selectItem.Tiles.First().Count);
                    }
                }
            }
            catch
            {
                return;
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
